package com.autonavi.collapse.task.context;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;

import java.util.Map;

public class SpringContextManager extends DefaultScanContextManager{

    private ApplicationContext applicationContext;

    public SpringContextManager(ApplicationContext applicationContext, String... packages) throws Exception {
        super(packages);
        this.applicationContext = applicationContext;
    }

    @Override
    public <T> T getComponentByType(Class<T> type) {
        T bean = null;
        try {
            bean = applicationContext.getBean(type);
        }catch (BeansException e) {

        }
        if (bean != null) {
            return bean;
        }
        return super.getComponentByType(type);
    }

    @Override
    public Object getComponentByName(String name) {
        Object bean = null;
        try {
            bean = applicationContext.getBean(name);
        }catch (BeansException e) {

        }
        if (bean != null) {
            return bean;
        }
        return super.getComponentByName(name);
    }

    @Override
    public <T> Map<String, T> getComponentsByType(Class<T> type) {
        Map<String, T> result = applicationContext.getBeansOfType(type);
        result.putAll(super.getComponentsByType(type));
        return result;
    }
}
